﻿using System;
using System.Web.UI;

namespace Triad.Controls
{
    /// <summary>
    /// Class is used as to consolidate access to resources (images/scripts, etc).
    /// </summary>
    public class tfResources
    {
        /// <summary>
        /// tfCore Constant
        /// </summary>
        public const string RljjQuery = "Triad.Controls.tfResources.rel_js.jquery-1.2.6.pack.js";
        /// <summary>
        /// tfCore Constant
        /// </summary>
        public const string RljAPPCOMMON = "Triad.Controls.tfResources.rel_js.appcommon.js";
        /// <summary>
        /// tfCore Constant
        /// </summary>
        public const string RljAPPMAIN = "Triad.Controls.tfResources.rel_js.appmain.js";
        /// <summary>
        /// tfCore Constant
        /// </summary>
        public const string RljCORE = "Triad.Controls.tfResources.rel_js.tfcore.js";
        /// <summary>
        /// ModalPopups Constant
        /// </summary>
        public const string RljMODALPOPUPS = "Triad.Controls.tfResources.rel_js.tfpopup.js";
        /// <summary>
        /// Notification Logging Constant
        /// </summary>
        public const string RljNOTIFICATIONS = "Triad.Controls.tfResources.rel_js.ednlog.js";
        /// <summary>
        /// tfListView Constant
        /// </summary>
        public const string RljLISTVIEW = "Triad.Controls.tfResources.rel_js.tflv.js";
        /// <summary>
        /// tfComoboBox Constant
        /// </summary>
        public const string RljCOMBOBOX = "Triad.Controls.tfResources.rel_js.edcb.js";
        /// <summary>
        /// tfTreeView Constant
        /// </summary>
        public const string RljTREEVIEW = "Triad.Controls.tfResources.rel_js.edtreeview.js";
        /// <summary>
        /// tfNlog CSS Constant
        /// </summary>
        public const string RlcNLOG = "Triad.Controls.tfResources.rel_css.ednlog.css";
        /// <summary>
        /// ModalDialogs CSS Constant
        /// </summary>
        public const string RlcMODALDIALOG = "Triad.Controls.tfResources.rel_css.edmodaldialog.css";

        public const string RlcCOMBOBOX = "Triad.Controls.tfResources.rel_css.tfCombobox.css";
        public const string RlcLISTVIEW = "Triad.Controls.tfResources.rel_css.tflview.css";

        /// <summary>
        /// Close Icon Image Constant
        /// </summary>
        public const string RliCLOSE = "Triad.Controls.tfResources.img.close.gif";
        /// <summary>
        /// tfCore Constant
        /// </summary>
        public const string RliSMCLOSE = "Triad.Controls.tfResources.img.smclose.gif";

        /// <summary>
        /// Small Close Icon Image Constant
        /// </summary>
        public const string RliINFO = "Triad.Controls.tfResources.img.information.jpg";
        /// <summary>
        /// Warning Image Constant
        /// </summary>
        public const string RliWARNING = "Triad.Controls.tfResources.img.warning.jpg";
        /// <summary>
        /// Error Image Constant
        /// </summary>
        public const string RliERROR = "Triad.Controls.tfResources.img.error.jpg";

        /// <summary>
        /// 'Small' Information Constant
        /// </summary>
        public const string RliSMINFO = "Triad.Controls.tfResources.img.sminformation.jpg";
        /// <summary>
        /// 'Small' Warning Constant
        /// </summary>
        public const string RliSMWARNING = "Triad.Controls.tfResources.img.smwarning.jpg";
        /// <summary>
        /// 'Small' Error Constant
        /// </summary>
        public const string RliSMERROR = "Triad.Controls.tfResources.img.smerror.jpg";

        /// <summary>
        /// ComboBox Image Constant
        /// </summary>
        public const string RliCBDOWN = "Triad.Controls.tfResources.img.xpbtn_n.gif";
        public const string RliGSFICON = "Triad.Controls.tfResources.img.gsficon.gif";

        /// <summary>
        /// Loads the script library.
        /// </summary>
        /// <param name="control">The control.</param>
        public static void LoadCoreScripts(Control control)
        {
            ClientScriptManager cm = control.Page.ClientScript;
            cm.RegisterClientScriptResource(typeof(tfResources), RljCORE);
            cm.RegisterClientScriptResource(typeof(tfResources), RljAPPCOMMON);
            string cssLink = "<link href='" + control.Page.ClientScript.GetWebResourceUrl(typeof(tfResources), RlcMODALDIALOG) + "' rel='stylesheet' type='text/css' />";
            LiteralControl lcLiteralControl = new LiteralControl(cssLink);
            control.Page.Header.Controls.Add(lcLiteralControl);
        }
        /// <summary>
        /// Loads the script library.
        /// </summary>
        /// <param name="control">The control.</param>
        public static void LoadMainMenuScripts(Control control)
        {

            ClientScriptManager cm = control.Page.ClientScript;
            cm.RegisterClientScriptBlock(typeof(Page), "tfcore", "", false);
            cm.RegisterClientScriptResource(typeof(tfResources), RljCORE);
            string cssLink = "<link href='" + control.Page.ClientScript.GetWebResourceUrl(typeof(tfResources), RlcNLOG) + "' rel='stylesheet' type='text/css' />";
            LiteralControl lcLiteralControl = new LiteralControl(cssLink);
            control.Page.Header.Controls.Add(lcLiteralControl);
            cssLink = "<link href='" + control.Page.ClientScript.GetWebResourceUrl(typeof(tfResources), RlcMODALDIALOG) + "' rel='stylesheet' type='text/css' />";
            lcLiteralControl = new LiteralControl(cssLink);
            control.Page.Header.Controls.Add(lcLiteralControl);
            // Create JS WebResourceLinks for Images
            string JSLink = "<script type='text/javascript'>";
            JSLink += string.Format("var edX=\"{0}\";", control.Page.ClientScript.GetWebResourceUrl(typeof(tfResources), RliCLOSE));
            JSLink += string.Format("var edXs=\"{0}\";", control.Page.ClientScript.GetWebResourceUrl(typeof(tfResources), RliSMCLOSE));
            JSLink += string.Format("var edI=\"{0}\";", control.Page.ClientScript.GetWebResourceUrl(typeof(tfResources), RliINFO));
            JSLink += string.Format("var gsfICON=\"{0}\";", control.Page.ClientScript.GetWebResourceUrl(typeof(tfResources), RliGSFICON));
            JSLink += string.Format("var edE=\"{0}\";", control.Page.ClientScript.GetWebResourceUrl(typeof(tfResources), RliWARNING));
            JSLink += string.Format("var edW=\"{0}\";", control.Page.ClientScript.GetWebResourceUrl(typeof(tfResources), RliERROR));
            JSLink += string.Format("var edIs=\"{0}\";", control.Page.ClientScript.GetWebResourceUrl(typeof(tfResources), RliSMINFO));
            JSLink += string.Format("var edEs=\"{0}\";", control.Page.ClientScript.GetWebResourceUrl(typeof(tfResources), RliSMWARNING));
            JSLink += string.Format("var edWs=\"{0}\";", control.Page.ClientScript.GetWebResourceUrl(typeof(tfResources), RliSMERROR));
            JSLink += "</script>";
            lcLiteralControl = new LiteralControl(JSLink);
            control.Page.Header.Controls.Add(lcLiteralControl);

            cm.RegisterClientScriptResource(typeof(tfResources), RljjQuery);
            cm.RegisterClientScriptResource(typeof(tfResources), RljAPPCOMMON);
            cm.RegisterClientScriptResource(typeof(tfResources), RljAPPMAIN);
            cm.RegisterClientScriptResource(typeof(tfResources), RljMODALPOPUPS);
            cm.RegisterClientScriptResource(typeof(tfResources), RljNOTIFICATIONS);
        }

        /// <summary>
        /// Encode Javascript string.
        /// </summary>
        /// <param name="value">original value.</param>
        /// <param name="forUrl">if set to <c>true</c> [for URL].</param>
        /// <returns></returns>
        /// <remarks>
        /// Author: tim.fischer,   8/25/2008 - 1:30 PM
        /// Modified By: 
        /// </remarks>
        public static string JScriptEncode(string value, Boolean forUrl)
        {
            if (String.IsNullOrEmpty(value)) return value;
            string sbuilder = value.Replace("%", "%%");
            sbuilder = sbuilder.Replace("&", "&amp;");
            sbuilder = sbuilder.Replace("\\", "\\\\");
            sbuilder = sbuilder.Replace("'", "\\'");
            sbuilder = sbuilder.Replace(Convert.ToString((char)9), "\\t");
            sbuilder = sbuilder.Replace(Convert.ToString((char)10), "\\n");
            sbuilder = sbuilder.Replace(Convert.ToString((char)13), "\\r");
            sbuilder = sbuilder.Replace(Convert.ToString((char)34), "\\" + Convert.ToString((char)34));
            sbuilder = sbuilder.Replace("<", "&lt");
            sbuilder = sbuilder.Replace(">", "&gt");
            //\' - single quote, needed for character literals 
            //\" - double quote, needed for string literals 
            //\\ - backslash 
            //\0 - Unicode character 0 
            //\a - Alert (character 7) 
            //\b - Backspace (character 8) 
            //\f - Form feed (character 12) 
            //\n - New line (character 10) 
            //\r - Carriage return (character 13) 
            //\t - Horizontal tab (character 9) 
            //\v - Vertical quote (character 11) 
            if (forUrl) sbuilder = sbuilder.Replace(" ", "%20");
            return sbuilder;
        }       
    
    }
}
